package com.cn.yz.springboot.resource;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.cn.yz.springboot.jersey.AcceptToken;
import com.cn.yz.springboot.jersey.HttpStatus;
import com.cn.yz.springboot.jersey.PagedData;
import com.cn.yz.springboot.jersey.ResponseObject;
import com.cn.yz.springboot.jersey.ResponseString;
import com.cn.yz.springboot.model.SysUser;
import com.cn.yz.springboot.service.SysUserService;

@Path("/")
@Component
public class AdminResource {

	@Autowired
    private SysUserService sysUserService;
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
    @Path("/findSysUser")
	public ResponseObject<SysUser> getSysUser(@QueryParam("id")Integer id){
		SysUser sysUser = sysUserService.selectByPrimaryKey(id);
		if(sysUser==null){
			return new ResponseObject<SysUser>(HttpStatus.EMPTY.getCode(),HttpStatus.EMPTY.getMsg(),sysUser);
		}
		return new ResponseObject<SysUser>(HttpStatus.OK.getCode(),HttpStatus.OK.getMsg(),sysUser);
	}
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
	@Path("/addSysUser")
	public ResponseString addSysUser(SysUser sysUser,@Context HttpServletRequest request){
		request.setAttribute("token", "qeejcurrhhvhhuu=!kkdfkfkfif");
		sysUserService.addSysUser(sysUser);
		return new ResponseString(HttpStatus.OK.getCode(),HttpStatus.OK.getMsg());
	}
	
	@POST
	@Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
	@Path("/login")
	public ResponseString login(SysUser sysUser,@Context HttpServletRequest request){
//		httpHeaders.add("token", "12hfdddjshffddkjjdj=-sh");
	
		return new ResponseObject<AcceptToken>(HttpStatus.OK.getCode(),"登录成功",new AcceptToken());
	}
	
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
    @Path("/querySysUser")
	public ResponseObject<PagedData<SysUser>> querySysUser(@QueryParam("account")String account,@QueryParam("pageIndex")Integer pageIndex,@QueryParam("pageSize")Integer pageSize){
		Map<String,Object> condition = new HashMap<String,Object>();
		if(!StringUtils.isEmpty(account)){
			condition.put("account", account);
		}
		if(pageIndex!=null && pageIndex>0){
			condition.put("pageIndex", pageIndex);
		}
		if(pageSize!=null && pageSize>0){
			condition.put("pageSize", pageSize);
		}
		PagedData<SysUser> page = sysUserService.querySysUser(condition);
		
		if(page.getRecordCount()==0){
			return new ResponseObject<PagedData<SysUser>>(HttpStatus.EMPTY.getCode(),HttpStatus.EMPTY.getMsg(),page);
		}
		return new ResponseObject<PagedData<SysUser>>(HttpStatus.OK.getCode(),HttpStatus.OK.getMsg(),page);
	}
}
